package data

import (
	"digitalhuman/internal/conf"
	"github.com/wenqingqaq/backend-lib/logz"
	"github.com/wenqingqaq/backend-lib/tecmq"
)

type NatsData struct {
	client *tecmq.NatJs
	conf   *conf.Data
}

func NewNatsData(conf *conf.Data) *NatsData {
	return &NatsData{
		client: NewNatsC(conf),
		conf:   conf,
	}
}

func NewNatsC(conf *conf.Data) *tecmq.NatJs {
	nj, err := tecmq.NewNatsJsSubPushQueueClient(conf.Nats.Url, conf.Nats.JsName, []string{conf.Nats.Subject, conf.Nats.CallbackSubject}) // 创建一个nats连接
	if err != nil {
		logz.Err("nats failed", err)
		panic(err)
	}

	return nj
}

// SendMsg 发送消息到js
func (n *NatsData) SendMsg(message []byte) error {
	return n.client.JsPushMessage(n.conf.Nats.Subject, message)
}
